package com.yanqu.road.server.gameplayer.module.player.beauty;

import com.yanqu.road.entity.config.beauty.BeautyInfo;
import com.yanqu.road.entity.config.skill.SkillInfo;
import com.yanqu.road.entity.config.skill.SkillUpgradeInfo;
import com.yanqu.road.entity.config.skin.SkinInfo;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.enums.eSkinType;
import com.yanqu.road.entity.player.UserBeauty;
import com.yanqu.road.entity.player.UserSkin;
import com.yanqu.road.entity.travelbook.UserTravelAlbum;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.travelbook.TravelBookModule;
import com.yanqu.road.server.gameplayer.module.player.BeautyModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.skin.SkinModule;
import com.yanqu.road.server.manger.config.BeautyMgr;
import com.yanqu.road.server.manger.config.SkillMgr;
import com.yanqu.road.server.manger.config.SkinMgr;
import com.yanqu.road.server.manger.config.TravelBookConfigMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.property.Property;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;

public class SkinAddOccupationQuality extends BeautyUpgradeSkillAction {

    public SkinAddOccupationQuality(int beautyId, int position, int skillType, int batch, BeautyModule beautyModule, GamePlayer player) {
        super(beautyId, position, skillType, batch, beautyModule, player);
    }

    @Override
    public void setOldLv() {
        UserBeauty userBeauty = beautyModule.getUserBeauty(beautyId);
        oldLv = userBeauty.getSkinSkill1();
    }

    @Override
    public SkillInfo getSkillInfo() {
        BeautyInfo beautyInfo = BeautyMgr.getBeautyInfo(beautyId);
        return SkillMgr.getSkillInfo(beautyInfo.getHaloSkill(0));
    }

    @Override
    public void calConsumer() {
        //等级上限
        int maxSkillLv = 0;
        Map<Integer, UserSkin> skinMap = player.getModule(SkinModule.class).getUserSkinMap();
        for(UserSkin userSkin : skinMap.values()){
            SkinInfo skinInfo = SkinMgr.getSkinInfo(userSkin.getSkinId());
            if(skinInfo.getType() == eSkinType.Beauty.getValue() && skinInfo.getBelongRole() == beautyId){
                int skillUpgradeType = skinInfo.getSkinBeautySkillUpgradeType(0);
                SkillUpgradeInfo skillUpgradeInfo = SkillMgr.getSkillUpgradeInfoIfNotGetMax(skillUpgradeType, userSkin.getLevel());
                if (null != skillUpgradeInfo) {
                    maxSkillLv += skillUpgradeInfo.getConsumeCount();
                }
            }
        }

        // 旅行册卡册等级增加仪容技能等级上限
        List<UserTravelAlbum> albums = player.getModule(TravelBookModule.class).getBeautyAlbumMap().get(beautyId);
        if (albums != null) {
            for (UserTravelAlbum album : albums) {
                int albumId = album.getAlbumId();
                maxSkillLv += TravelBookConfigMgr.getSkillExtendLevel(albumId, album.getLevel(), getSkillInfo().getId());
            }
        }

        UserBeauty userBeauty = beautyModule.getUserBeauty(beautyId);
        if (userBeauty.getSkinSkill1() >= maxSkillLv) {
            this.ret = GameErrorCode.E_BEAUTY_LEVEL_MAX;
        }else {

            int upAble = Math.min(this.batch, maxSkillLv - userBeauty.getSkinSkill1());

            SkillInfo skillInfo = getSkillInfo();
            int itemId = skillInfo.getConsumeGoodsId().get(0);
            //计算消耗
            long totalNeed = 0;
            for (int i = 0; i < upAble; i++) {
                //当前等级需要的消耗
                SkillUpgradeInfo consume = SkillMgr.getSkillUpgradeInfo(skillInfo.getUpgradeType(0), this.oldLv + i);
                if(consume == null){
                    break;
                }
                if(!player.getModule(CurrencyModule.class).currencyIsEnough(new Property(itemId,totalNeed + consume.getConsumeCount()))){
                    //道具不足
                    break;
                }else {
                    totalNeed += consume.getConsumeCount();
                    this.realBatch++;
                }
            }

            this.needConsume = totalNeed;
            if(this.needConsume > 0){
                this.consumer = new Property(itemId, BigInteger.valueOf(this.needConsume));
            }
            if(this.realBatch <= 0){
                this.ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
            }
        }

    }

    @Override
    public void upgrade() {
        preValidArgument();

        if(this.ret == 0) {
            checkMaxLv();
        }

        if(this.ret == 0){
            calConsumer();
        }
        if(this.ret == 0) {
            UserBeauty userBeauty = beautyModule.getUserBeauty(beautyId);
            if (!player.getModule(CurrencyModule.class).removeCurrency(consumer, eLogMoneyType.Beauty, eLogMoneyType.BeautySkinSkillUpgrade)) {
                this.ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;          //升级道具不足
            }else {
                userBeauty.setSkinSkill1(userBeauty.getSkinSkill1() + this.realBatch);
            }
        }
    }
}
